﻿using System.Windows;
using System.Windows.Controls;
using System;
using WPFExcelReport;
namespace WPFExcelReport.Controls
{
    /// <summary>
    /// Interaction logic for Machine.xaml
    /// </summary>

    public partial class MCALENDAR : ViewBase
    {
        #region ==MachineID===========
        public static readonly DependencyProperty MachineIDProperty = DependencyProperty.Register("MachineID",
            typeof(string), typeof(MCALENDAR),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMachineIDChanged));
        public string MachineID
        {
            get
            {
                return (string)GetValue(MachineIDProperty);
            }
            set
            {
                SetValue(MachineIDProperty, value);
                OnMachineIDChg();
            }
        }
        private static void OnMachineIDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MCALENDAR;
            if (src != null)
            {
                src.MachineID = (string)e.NewValue;
            }
        }
        public virtual void OnMachineIDChg()
        {
            this.Name = MachineID;
        }
        #endregion 
        #region ==GDays===========
        public static readonly DependencyProperty GDaysProperty = DependencyProperty.Register("GDays",
            typeof(double), typeof(MCALENDAR),
            new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGDaysChanged));
        public double GDays
        {
            get
            {
                return (double)GetValue(GDaysProperty);
            }
            set
            {
                SetValue(GDaysProperty, value);
                OnGDaysChg();
            }
        }
        private static void OnGDaysChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MCALENDAR;
            if (src != null)
            {
                src.GDays = (double)e.NewValue;
            }
        }
        public virtual void OnGDaysChg()
        {
            SetUp();
        }
        #endregion

        #region ==HistoryDays===========
        public static readonly DependencyProperty HistoryDaysProperty = DependencyProperty.Register("HistoryDays",
            typeof(double), typeof(MCALENDAR),
            new FrameworkPropertyMetadata(3.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHistoryDaysChanged));
        public double HistoryDays
        {
            get
            {
                return (double)GetValue(HistoryDaysProperty);
            }
            set
            {
                SetValue(HistoryDaysProperty, value);
                OnHistoryDaysChg();
            }
        }
        private static void OnHistoryDaysChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MCALENDAR;
            if (src != null)
            {
                src.HistoryDays = (double)e.NewValue;
            }
        }
        public virtual void OnHistoryDaysChg()
        {
            SetUp();
        }
        #endregion


        public DateTime CalendarStart
        {
            get;
            set;
        }
       
        public DateTime NOW
        {
            get;
            set;
        }
        private double _GDensity = 2;
        public double GDensity
        {
            get
            {
                return _GDensity;
            }
            set
            {
                _GDensity = value;
                PIXELS = Math.Round((30 *  2 / GDensity), 0);
               
            }
        }
        public int CurrentBlock
        {
            get;
            set;
        }
        private double  _PIXELS=1440;
        public double PIXELS
        {
            get
            { 
                 return _PIXELS;
            } 
            set
            {
                _PIXELS = value;
                ReSetGrid();
            }
        }
        public double TotalBlocks
        {
            get;
            set;
        }
       
        public MCALENDAR()
        {
            InitializeComponent();
            DataContext = this;
            HistoryDays = 0;
        }
        /// <summary>
        /// 1.一个显示单元格就是30个像素
        /// 2.至于30个像素代表多长时间，可以调整
        /// 3.每隔2个小时一个点
        /// </summary>
        public void SetUp()
        {
            if (HistoryDays == 0)
                return;
            this.Name = MachineID; 
            TotalBlocks = 0;
            ColumnDefinition cd;            
            CalendarStart = new DateTime(NOW.Year, NOW.Month, NOW.Day).AddDays(0-HistoryDays);
            int month = CalendarStart.Month;
            int year = CalendarStart.Year;
            MGrid.ColumnDefinitions.Clear();
            MGrid.Children.Clear();
            cd = new ColumnDefinition();
            cd.Width = new GridLength(5);
            MGrid.ColumnDefinitions.Add(cd);
            for (int i = 1; i < GDays + HistoryDays; i++)
            {
                int blocks = 24;

                blocks = 24;
                for (int j = 0; j < blocks; j++)
                {
                    TotalBlocks++;
                    cd = new ColumnDefinition();
                    cd.Width = new GridLength(PIXELS);
                    MGrid.ColumnDefinitions.Add(cd);
                } 
                DateTime tDate = CalendarStart.AddDays(i - 1);
                if (tDate.Date == NOW.Date)
                {
                    CurrentBlock = (i - 1) * 24 + NOW.Hour + 1;
                }
                string mark = tDate.Day.ToString();
                if (tDate.Year > year)
                {
                    year = tDate.Year;
                    month = 1;
                    mark = year.ToString() + "年";
                }
                if (tDate.Month > month)
                {
                    month = tDate.Month;
                    mark = month.ToString() + "月";
                }
                TextBlock tx = new TextBlock();
                tx.Text = mark;
                tx.HorizontalAlignment = HorizontalAlignment.Left;
                tx.SetValue(Grid.ColumnProperty, 1 + (i - 1) * 24);
                tx.SetValue(Grid.ColumnSpanProperty, 24);
                MGrid.Children.Add(tx);

                
            }
        }
        public int GetPosition(DateTime _StartDate)
        {
            int StartPoint = 1;
            StartPoint = Convert.ToInt32(_StartDate.Subtract(CalendarStart).TotalHours + 1);
            if (StartPoint < 1)
                StartPoint = 1; 
            //this.Processed.Width = continus * 30;
            StartPoint = Convert.ToInt32(StartPoint);
            return StartPoint;
            //this.SetValue(Grid.RowProperty, 0);
            //this.SetValue(Grid.ColumnProperty, GridColumn);
            //this.SetValue(Grid.ColumnSpanProperty, GridColumContinues);
        }
        private int WeekDay(DateTime dTime)
        {
            int weekIndex = 1;
            try
            {
                //确定此时间在一年中的位置  
                var dayOfYear = dTime.DayOfYear;
                //当年第一天  
                var tempDate = new DateTime(dTime.Year, 1, 1);
                //确定当年第一天  
                var tempDayOfWeek = (int)tempDate.DayOfWeek;
                tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
                //确定星期几  
                var index = (int)dTime.DayOfWeek;
                index = index == 0 ? 7 : index;
                //当前周的范围  
                DateTime retStartDay = dTime.AddDays(-(index - 1));
                DateTime retEndDay = dTime.AddDays(7 - index);
                //确定当前是第几周  
                weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
                if (retStartDay.Year < retEndDay.Year)
                {
                    weekIndex = 1;
                }
            }
            catch (Exception ex)
            {
            }
            return weekIndex; 
        }
        public void ReSetGrid()
        { 
            foreach (ColumnDefinition cd in MGrid.ColumnDefinitions)
            {
                cd.Width = new GridLength(PIXELS);
            }
        }
    }
}
